//
//  LoadingView.m
//  HouseOfWillow
//
//  Created by ODSdev2 on 15/10/23.
//  Copyright (c) 2015年 liubenwang. All rights reserved.
//

#import "LoadingView.h"
#import <QuartzCore/QuartzCore.h>
#import "FXBlurView.h"
@interface LoadingView()

@property (nonatomic, strong) FXBlurView * blurView;/**<*/
@end
@implementation LoadingView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _blurView = [[FXBlurView alloc]initWithFrame:self.bounds];
        [_blurView setBlurEnabled:YES];
        [_blurView setBlurRadius:10];
        [_blurView setTintColor:[UIColor blackColor]];
        _blurView.dynamic = YES;
        [self addSubview:_blurView];
        [self performSelector:@selector(addScreenShot) withObject:nil afterDelay:1.0];
        [self addLoading];
    }
    return self;
}
- (void)addScreenShot{
    
//    if (self) {
//        UIImage * image = [UIImage imageScreenView:RootWindow.subviews.lastObject];
//        _imageView.image = image.blurImage();
//    }
}
- (void)addLoading{
    
//    UIImageView * iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SIZETO6PlusW(237), SIZETO6PlusW(237))];
//    iconImg.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-iconImg.height/4);
//    iconImg.image = [UIImage imageNamed:@"加载中"];
//    [self addSubview:iconImg];
    
    
    UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    //<2>设置活动指示器初始状态为隐藏
//    activity.hidden = YES;
    //<3>设置活动指示器停止活动的时候 隐藏
    activity.hidesWhenStopped = YES;
    //<4>设置活动指示器的显示位置
    activity.frame = CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT - 100)/2, 100, 100);
//    activity.tag = 200;
    [self addSubview:activity];
    [activity startAnimating];//开始动画

    
}
+ (void)loadingView{

    if (!RootWindow) {
        RootWindow = [UIApplication sharedApplication].keyWindow;
    }
    for (UIView * sub in RootWindow.subviews) {
        if ([sub isKindOfClass:[LoadingView class]]) {
            return;
        }
    }
    LoadingView * background = [[LoadingView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    background.backgroundColor = [UIColor clearColor];
    [RootWindow addSubview:background];
}
+ (void)removeLoadingView{

    if (!RootWindow) {
        RootWindow = [UIApplication sharedApplication].keyWindow;
    }
    for (UIView * sub in RootWindow.subviews) {
        if ([sub isKindOfClass:[LoadingView class]]) {
            [UIView animateWithDuration:0.2 animations:^{
                sub.alpha = 0;
            } completion:^(BOOL finished) {
                [sub removeFromSuperview];
            }];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
